package com.td.module.equipment.service.waterpump;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.checksum.crc16.CRC16Modbus;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.NumberUtil;
import com.td.framework.common.pojo.PageResult;
import com.td.module.equipment.controller.admin.waterpump.vo.PumpPageReqVO;
import com.td.module.equipment.controller.admin.waterpump.vo.PumpUpdateReqVO;
import com.td.module.equipment.convert.waterpump.PumpConvert;
import com.td.module.equipment.dal.dataobject.waterpump.PumpDO;
import com.td.module.equipment.dal.mysql.waterpump.PumpMapper;
import com.td.module.equipment.framework.config.MqttConfig;
import com.td.module.equipment.framework.core.MqttClientUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.equipment.enums.ErrorCodeConstants.PUMP_NOT_EXISTS;

/**
 * 水泵信息 Service 实现类
 *
 * @author 王翔翔
 */
@Service
@Validated
public class PumpServiceImpl implements PumpService {

    @Resource
    private PumpMapper pumpMapper;

    @Resource
    private MqttConfig mqttConfig;

    @Override
    public void updatePump(PumpUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePumpExists(updateReqVO.getId());
        PumpDO pumpDO = this.getPump(updateReqVO.getId());
        // 更新
        PumpDO updateObj = PumpConvert.INSTANCE.convert(updateReqVO);

        StringBuffer hexStr = new StringBuffer("01060001");

        //计算下发阀位值
        Integer valveValue = updateReqVO.getValveValue() * 10 + 1999;
        String hexValveValue = String.format("%04x", valveValue);
        hexStr.append(hexValveValue);

        // crc校验
        CRC16Modbus crc16Modbus = new CRC16Modbus();
        crc16Modbus.update(HexUtil.decodeHex(hexStr));
        String crcResult = crc16Modbus.getHexValue(true).toUpperCase();
        // 高低位交换
        String crcSwapResult = crcResult.substring(2, 4) + crcResult.substring(0, 2);

        hexStr.append(crcSwapResult);

        String mesStr = "{\"FUNC\":\"MES_DOWN\",\"INFO\":{\"NODE_NUM\":1,\"MES_STR\":\"" + Base64.encode(HexUtil.decodeHex(hexStr)) + "\"}}";

        String topic = "api/v1/dtu_lhy/" + pumpDO.getTopicKey() + "/down";

        // 调用mqtt接口
        String clientId = mqttConfig.getClientList().get(1).getClientId();
        MqttClientUtil.publish(clientId, topic, mesStr, 0);

        pumpMapper.updateById(updateObj);
    }

    @Override
    public void deletePump(Long id) {
        // 校验存在
        this.validatePumpExists(id);
        // 删除
        pumpMapper.deleteById(id);
    }

    private void validatePumpExists(Long id) {
        if (pumpMapper.selectById(id) == null) {
            throw exception(PUMP_NOT_EXISTS);
        }
    }

    @Override
    public PumpDO getPump(Long id) {
        return pumpMapper.selectById(id);
    }

    @Override
    public List<PumpDO> getPumpList(Collection<Long> ids) {
        return pumpMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PumpDO> getPumpPage(PumpPageReqVO pageReqVO) {
        return pumpMapper.selectPage(pageReqVO);
    }

}
